package com.example.smart_city_0829.activity.pet_hos;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.Base;
import com.example.smart_city_0829.entity.petHos.InquiryMessageList;
import com.example.smart_city_0829.entity.petHos.PubInquiryList;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeLv;

import java.util.HashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;

public class InterrogationDetailActivity extends BaseActivity {

    private CardView cardView;
    private ImageView queryIm;
    private TextView queryContent;
    private CardView cardView2;
    private ImageView imageView;
    private TextView nameTv;
    private TextView jobNameTv;
    private TextView practiceNoTv;
    private LinearLayout linearLayout3;
    private MaxSizeLv msgLv;
    private EditText quesEt;
    private Button sendBt;
    private Button postQuesBt;


    private LvAdapter lvAdapter;

    private PubInquiryList.RowsBean inquiryData;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_interrogation_detail;
    }

    @Override
    protected void initView() {
        cardView = (CardView) findViewById(R.id.cardView);
        queryIm = (ImageView) findViewById(R.id.query_im);
        queryContent = (TextView) findViewById(R.id.query_content);
        cardView2 = (CardView) findViewById(R.id.cardView2);
        imageView = (ImageView) findViewById(R.id.imageView);
        nameTv = (TextView) findViewById(R.id.name_tv);
        jobNameTv = (TextView) findViewById(R.id.jobName_tv);
        practiceNoTv = (TextView) findViewById(R.id.practiceNo_tv);
        linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3);
        msgLv = (MaxSizeLv) findViewById(R.id.msg_lv);
        quesEt = (EditText) findViewById(R.id.ques_et);
        sendBt = (Button) findViewById(R.id.send_bt);
        postQuesBt = (Button) findViewById(R.id.post_ques_bt);

    }

    @Override
    protected void initData() {
        inquiryData = (PubInquiryList.RowsBean) getIntent().getSerializableExtra("inquiryData");
        int type = getIntent().getIntExtra("type",0);

        if (type == 0){
            quesEt.setVisibility(View.VISIBLE);
            sendBt.setVisibility(View.VISIBLE);
        }else if (type == 1){
            postQuesBt.setVisibility(View.VISIBLE);
        }

        if (!TextUtils.isEmpty(inquiryData.getImageUrls())){
            final String[] split = inquiryData.getImageUrls().split(",");
            Glide.with(context)
                    .load(Constant.BASE_URL + split[0])
                    .into(queryIm);
        }
        queryContent.setText(String.valueOf(inquiryData.getQuestion()));

        if (inquiryData.getDoctor() != null){
            Glide.with(context)
                    .load(Constant.BASE_URL + inquiryData.getDoctor().getAvatar())
                    .into(imageView);
            nameTv.setText(String.valueOf(inquiryData.getDoctor().getName()));
            jobNameTv.setText(String.valueOf(inquiryData.getDoctor().getJobName()));
            practiceNoTv.setText(String.valueOf(inquiryData.getDoctor().getPracticeNo()));
        }

        getMessageList();
    }

    @Override
    protected void initEvent() {
        sendBt.setOnClickListener(v -> {
            final String s = quesEt.getText().toString();
            quesEt.setText("");
            if (!TextUtils.isEmpty(s)){
                final HashMap<String, Object> params = new HashMap<>();
                params.put("inquiryId",inquiryData.getId());
                params.put("content",s);
                api.postInquiryMessage(
                        RequestBody.create(gson.toJson(params), MediaType.parse("application/json"))
                ).enqueue(new NetCallBack<Base>() {
                    @Override
                    public void onResponse(Call<Base> call, Response<Base> response) {
                        if (response.body() == null) return;

                        if (response.body().getCode() == 200) {
                            showToastShort("追问成功");
                            getMessageList();
                        }else {
                            showToastShort("追问失败");
                        }
                    }
                });
            }else {
                showToastShort("请输入提问内容");
            }
        });

        postQuesBt.setOnClickListener((v) -> {
            final Intent intent = new Intent(context, InterrogationActivity.class);
            intent.putExtra("doc",gson.toJson(inquiryData.getDoctor()));
            startActivity(intent);
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "问诊详情";
    }

    private void getMessageList(){
        api.getInquiryMessageList(inquiryData.getId()).enqueue(new NetCallBack<InquiryMessageList>() {
            @Override
            public void onResponse(Call<InquiryMessageList> call, Response<InquiryMessageList> response) {
                if (response.body() == null) return;

                final List<InquiryMessageList.RowsBean> rows = response.body().getRows();
                if (lvAdapter == null){
                    lvAdapter = new LvAdapter(rows,context);
                    msgLv.setAdapter(lvAdapter);
                }else {
                    lvAdapter.resetData(rows);
                    lvAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    static class LvAdapter extends CommonAdapter<InquiryMessageList.RowsBean,CommonAdapter.CommentListViewHolder>{

        public LvAdapter(List<InquiryMessageList.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(InquiryMessageList.RowsBean data, int position, CommentListViewHolder viewHolder, View convertView, Context context) {
            viewHolder.cb.setVisibility(View.GONE);

            if (Integer.parseInt(data.getFromRole()) == 0){
                viewHolder.name.setText("问诊人");
            }else {
                viewHolder.name.setText("医生");
            }
            viewHolder.content.setText(data.getContent());
            viewHolder.tag1.setText(data.getCreateTime());
        }

        @Override
        protected CommentListViewHolder getViewHolder() {
            return new CommentListViewHolder();
        }

        @Override
        protected void initView(CommentListViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
            viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cb);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.comment_list_item;
        }
    }
}